# -*- coding: utf-8 -*-
#
# Copyright 2008 Mikael Schönenberg
#
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
#

VERSION			= 0.3

DOWNLOAD		= download

BUILD			= build
PACKAGES		= packages

BUILD_WINDOWS		= $(BUILD)/win32
BUILD_LINUX		= $(BUILD)/linux

PACKAGE			= kopsimo-$(VERSION)

PACKAGE_WINDOWS		= $(BUILD_WINDOWS)/$(PACKAGE)
PACKAGE_LINUX		= $(BUILD_LINUX)/$(PACKAGE)

PYINSTALLER_VER		= pyinstaller-1.3
PYINSTALLER_DIR		= pyinstaller
PYINSTALLER_WIN		= $(PYINSTALLER_DIR)/win
PYINSTALLER_LINUX	= $(PYINSTALLER_DIR)/linux

# pylint is a python source code analyzer that can be downloaded at
# http://www.logilab.org/project/name/pylint
PYLINT			= pylint

# py.test is part of pylib and can be downloaded at http://codespeak.net/
PYTEST			= ../py/py/bin/py.test

# path in which to (possibly install wine and) run windows tests
export WINEPREFIX	= $(PWD)/wine

tests:	test/report test_integration/report tests_windows

all:	tests pylint/report

clean:	clean-build clean-packages clean-test
	find . -name \*~ -exec rm {} \;
	find . -name \*.pyc -exec rm {} \;

squeaky-clean: clean clean-download clean-pyinstaller clean-wine

pylint/report: kopsimo.py pylint/rc
	PYTHONPATH=. $(PYLINT) --rcfile pylint/rc kopsimo.py | tee $@

clean-test:
	rm -f test/report
	rm -f test/report_win
	rm -f test_integration//report
	rm -f test_integration//report_win

test/report: kopsimo.py test/test_*.py
	PYTHONPATH=. $(PYTEST) -v --resultlog=$@ test/

test_integration/report: kopsimo.py test_integration/test_*.py
	PYTHONPATH=. $(PYTEST) -v --resultlog=$@ test_integration/

tests_windows:	test/report_win test_integration/report_win

test/report_win: py-win kopsimo.py test/test_*.py
	PYTHONPATH=. wine $(WINEPREFIX)/drive_c/Python25/python.exe $(PYTEST) -v --resultlog $@ test/

test_integration/report_win: py-win kopsimo.py test/test_*.py
	PYTHONPATH=. wine $(WINEPREFIX)/drive_c/Python25/python.exe $(PYTEST) -v --resultlog $@ test_integration/

clean-download:
	test -d $(DOWNLOAD) && rm -r $(DOWNLOAD) || true


# PyInstaller
clean-pyinstaller:
	test -d $(PYINSTALLER_DIR) && rm -r $(PYINSTALLER_DIR) || true

$(DOWNLOAD)/$(PYINSTALLER_VER).tar.gz:
	mkdir -p $(DOWNLOAD)
	wget -q -O $@ http://pyinstaller.hpcf.upr.edu/source/1.3/pyinstaller_1.3.tar.gz

$(PYINSTALLER_LINUX)/$(PYINSTALLER_VER): $(DOWNLOAD)/$(PYINSTALLER_VER).tar.gz
	mkdir -p $(PYINSTALLER_LINUX)
	tar -C $(PYINSTALLER_LINUX) -xzf $(DOWNLOAD)/$(PYINSTALLER_VER).tar.gz
	python $(PYINSTALLER_LINUX)/$(PYINSTALLER_VER)/source/linux/Make.py
	make -C $(PYINSTALLER_LINUX)/$(PYINSTALLER_VER)/source/linux
	python $(PYINSTALLER_LINUX)/$(PYINSTALLER_VER)/Configure.py
	# Compile all python modules, as PyInstaller does an odd exec
	# invokation that is not agnostic to the Win32 style line endings in
	# some of the files
	python -c "import compileall; compileall.compile_dir(\"$(PYINSTALLER_LINUX)/$(PYINSTALLER_VER)\");"

$(PYINSTALLER_WIN)/$(PYINSTALLER_VER): $(DOWNLOAD)/$(PYINSTALLER_VER).tar.gz
	mkdir -p $(PYINSTALLER_WIN)
	tar -C $(PYINSTALLER_WIN) -xzf $(DOWNLOAD)/$(PYINSTALLER_VER).tar.gz
	wine $(WINEPREFIX)/drive_c/Python25/python.exe $(PYINSTALLER_WIN)/$(PYINSTALLER_VER)/Configure.py

# Building
clean-build:
	test -d $(BUILD) && rm -r $(BUILD) || true

$(BUILD_LINUX)/kopsimo: $(PYINSTALLER_LINUX)/$(PYINSTALLER_VER) kopsimo.py kopsimo.spec
	mkdir -p $(BUILD_LINUX)
	cp -a kopsimo.spec $(BUILD_LINUX)/kopsimo.spec
	python $(PYINSTALLER_LINUX)/$(PYINSTALLER_VER)/Build.py $(BUILD_LINUX)/kopsimo.spec

$(BUILD_WINDOWS)/kopsimo.exe: py-win $(PYINSTALLER_WIN)/$(PYINSTALLER_VER) kopsimo.py kopsimo.spec
	mkdir -p $(BUILD_WINDOWS)
	cp -a kopsimo.spec $(BUILD_WINDOWS)/kopsimo.spec
	wine $(WINEPREFIX)/drive_c/Python25/python.exe $(PYINSTALLER_WIN)/$(PYINSTALLER_VER)/Build.py $(BUILD_WINDOWS)/kopsimo.spec

# Packaging
clean-packages:
	test -d $(PACKAGES) && rm -r $(PACKAGES) || true

$(PACKAGES)/$(PACKAGE).tar.bz2: $(BUILD_LINUX)/kopsimo
	test -n "$(VERSION)"
	mkdir -p $(PACKAGES) $(PACKAGE_LINUX)
	cp -a $(BUILD_LINUX)/kopsimo $(PACKAGE_LINUX)
	cp -a CHANGELOG $(PACKAGE_LINUX)
	cp -a COPYING $(PACKAGE_LINUX)
	cp -a README $(PACKAGE_LINUX)
	tar -C $(BUILD_LINUX) -cjf $@ $(PACKAGE)

$(PACKAGES)/$(PACKAGE).zip: $(BUILD_WINDOWS)/kopsimo.exe
	test -n "$(VERSION)"
	mkdir -p $(PACKAGES) $(PACKAGE_WINDOWS)
	cp -a $(BUILD_WINDOWS)/kopsimo.exe $(PACKAGE_WINDOWS)
	sed 's/$$/\r/' CHANGELOG > $(PACKAGE_WINDOWS)/CHANGELOG
	sed 's/$$/\r/' COPYING > $(PACKAGE_WINDOWS)/COPYING
	sed 's/$$/\r/' README > $(PACKAGE_WINDOWS)/README
	( cd $(BUILD)/win32 ; zip -r $(PACKAGE).zip kopsimo-$(VERSION) )
	cp -a $(BUILD)/win32/$(PACKAGE).zip $@

package: $(PACKAGES)/$(PACKAGE).tar.bz2 $(PACKAGES)/$(PACKAGE).zip


# Targets for Win32
clean-wine:
	test -d $(WINEPREFIX) && rm -r $(WINEPREFIX) || true

py-win: $(WINEPREFIX)/drive_c/Python25/python.exe

$(DOWNLOAD)/python-2.5.2.msi:
	mkdir -p $(DOWNLOAD)
	wget -q -O $@ http://www.python.org/ftp/python/2.5.2/python-2.5.2.msi

$(WINEPREFIX)/drive_c/Python25/python.exe: $(DOWNLOAD)/python-2.5.2.msi
	mkdir -p $(WINEPREFIX)
	wine msiexec /qn /i $(DOWNLOAD)/python-2.5.2.msi
	touch $@
